package com.h3c.nem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.CustomBeanUtils;
import com.h3c.nem.entity.TbOfferWorker;
import com.h3c.nem.entity.TbOfferWorkerHis;
import com.h3c.nem.enums.OfferWorkerStatusEnum;
import com.h3c.nem.mapper.TbOfferWorkerHisMapper;
import com.h3c.nem.mapper.TbOfferWorkerMapper;
import com.h3c.nem.service.TbOfferWorkerHisService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author f23104
* @description 针对表【tb_offer_worker_his(预备员工基本信息历史数据备份)】的数据库操作Service实现
* @createDate 2022-08-18 14:02:12
*/
@Service
public class TbOfferWorkerHisServiceImpl extends ServiceImpl<TbOfferWorkerHisMapper, TbOfferWorkerHis>
    implements TbOfferWorkerHisService{

    @Resource
    TbOfferWorkerMapper tbOfferWorkerMapper;

    public void moveWorkerToHisSingleForCancel(TbOfferWorker oriData) {
        oriData.setWorkerStatus(OfferWorkerStatusEnum.取消入职);
        moveWorkerToHis(Collections.singletonList(oriData));
    }

    @Transactional(rollbackFor = Exception.class)
    public void moveWorkerToHis(List<TbOfferWorker> oriData) {
        Date executeTime = new Date();
        List<TbOfferWorkerHis> saveList = new ArrayList<>();
        oriData.forEach(ori -> {
            TbOfferWorkerHis saveHis = new TbOfferWorkerHis();
            CustomBeanUtils.copyProperties(ori, saveHis);
            saveHis.setUpdateTime(executeTime);
            saveList.add(saveHis);
        });
        saveBatch(saveList);
        List<String> collect = oriData.stream().map(TbOfferWorker::getWorkerId).collect(Collectors.toList());
        tbOfferWorkerMapper.deleteBatchIds(collect);
    }

}




